package com.yiwei.ydd.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Spanned;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.ydd.R;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/4/24.
 */

public class LevelUpDialog extends Dialog {


    @BindView(R.id.img_top)
    ImageView imgTop;
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    private Context context;
    private int x24;

    public LevelUpDialog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public LevelUpDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        x24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_vip_level_up);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = (int) (Util.getDisplayMetrics(context).widthPixels * 0.85);
        mWindow.setAttributes(lp);


    }

    public LevelUpDialog setData(String id, String sale, String term_sale, String uolevel_rate, String utlevel_rate) {
        if (id.equals("2")){
            imgTop.setImageResource(R.mipmap.img_level_up_gold);
        }else if(id.equals("3")){
            imgTop.setImageResource(R.mipmap.img_level_up_bai);
        }
            txtContent.setText(getTextBottom(sale, term_sale, uolevel_rate, utlevel_rate));
        return this;
    }

    private Spanned getTextBottom(String sale, String term_sale, String uolevel_rate, String utlevel_rate) {
        return Spans.with(context).font("1.即时充值可享受最低", x24, 0xff333333).
                font(Float.valueOf(sale) / 10 + "", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("折优惠；\n2.购买加油套餐最多可享受赠送", x24, 0xff333333).
                font(term_sale + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("的充值返现；\n3.分享推荐拿佣金提成：一级奖励", x24, 0xff333333).
                font(uolevel_rate + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("，二级奖励", x24, 0xff333333).
                font(utlevel_rate + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("。", x24, 0xff333333).
                build();
    }

    private DialogDefaultClickListener listener;

    public LevelUpDialog setDialogClickListener(DialogDefaultClickListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        if(listener!=null){
            listener.confirm();
        }
        dismiss();
    }


    public interface DialogDefaultClickListener {
        public abstract void confirm();
    }
}
